/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.minhphuong.dal;

import com.minhphuong.entities.*;
import com.minhphuong.helper.Helper;

import java.util.List;
import javax.servlet.http.HttpSession;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author QuocMinh
 */
public class NguoiDungDAL {

    public static List<NguoiDung> getAllNguoiDung() {
        List<NguoiDung> listNguoiDung = null;
        Session session = null;

        try {
            session = HelperDAL.GetSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from NguoiDung");
            listNguoiDung = (List<NguoiDung>) q.list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return listNguoiDung;
    }

    public static boolean insertNguoiDung(NguoiDung nguoidung) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HelperDAL.GetSession();
            tx = session.beginTransaction();
            session.save(nguoidung);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
            return false;
        } finally {
            session.flush();
            session.close();
        }
        return true;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public static String create_captcha(int n) {
        String salt = "abcdefghijklmnopqrstuvwxyz0123456789";
        int i = 0;
        String new_captcha = "";
        String tmp;
        while (i < n) {
            int number = (int) (Math.random() * 36);
            tmp = salt.substring(number, number + 1);
            new_captcha = new_captcha + tmp;
            i++;
        }
        return new_captcha;
    }
public static NguoiDung GetUser_Login(String email, String matKhau){
        NguoiDung u = null;
        Session session = null;
        try {                    
            session = HelperDAL.GetSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery ("from NguoiDung where email = '" + email + "' and matKhau = '" + matKhau + "'");                        
            u = (NguoiDung) q.list().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            session.flush();
            session.close();
        }
        return u;
    }
    public static NguoiDung checkLogin(String email, String matKhau) {
        Session session = null;
        Transaction tx = null;
        NguoiDung nguoiDung = null;
        try {
            session = HelperDAL.GetSession();
            tx = session.beginTransaction();
            String query = "from NguoiDung where email=\"" + email + "\" and matKhau=\"" + matKhau + "\"";
            Query q = session.createQuery(query);
            if (q.list().size() > 0) {
                nguoiDung = (NguoiDung) q.list().get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (nguoiDung != null) {
                Hibernate.initialize(nguoiDung.getQuyen());
            }
            session.flush();
            session.close();
        }
        return nguoiDung;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    public static NguoiDung checkSession(HttpSession session) {
        if (session.getAttribute("nguoiDung") != null) {
            return (NguoiDung) session.getAttribute("nguoiDung");
        }
        return null;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    public static NguoiDung getNguoiDungByID(int id) {
        NguoiDung nguoidung = null;
        Session session = null;

        try {
            session = HelperDAL.GetSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from NguoiDung where maNd=:id").setInteger("id", id);
            nguoidung = (NguoiDung) q.list().get(0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            
            Hibernate.initialize(nguoidung.getQuyen());
            session.flush();
            session.close();
        }
        return nguoidung;
        //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    public static void updateNguoiDung(NguoiDung nguoidung){
        Session session = HelperDAL.GetSession();
        Transaction tx = null;
        try {
            tx = session.beginTransaction();    
            session.update(nguoidung);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (tx != null) {
                tx.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }
}
